//
//  UserCenter_ViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UserCenter_ViewController.h"
#import "LoadResigter_ViewController.h" // 登录和注册界面
#import "HadLogin_ViewController.h"  // 已经登录进入用户信息界面
#import "Find_ViewController.h" // 返回用

@interface UserCenter_ViewController ()


@property(nonatomic, strong) UIImageView *defaultLoadImageView;
@property(nonatomic, strong) UIButton *publishButton;
@property(nonatomic, strong) UIButton *collectionButton;
@property(nonatomic, strong) UIButton *historyButton;

@property(nonatomic, strong) UILabel *userNameLabel;

@end

@implementation UserCenter_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Controller的配置
    [self config];
    
    // 上半部分登录界面
    [self creatUpHalf];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    self.userNameLabel.text =userName;

    
    if([EMClient sharedClient].isLoggedIn){
      
        
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            
            
            NSString *userName = [[EMClient sharedClient] currentUsername];
            NSString *file = [path stringByAppendingPathComponent:userName];
        
            NSData *imageData = [NSData dataWithContentsOfFile:file];
            
            
            if (imageData == NULL)
            {
                
                self.defaultLoadImageView.image = [UIImage imageNamed:@"headerLogin.png"];
                
            }
            else
            {
                
                self.defaultLoadImageView.image = [UIImage imageWithData:imageData];
            }
        
        
       
    }else
    {
         self.defaultLoadImageView.image = [UIImage imageNamed:@"defaultIcon@3x.png"];
    }
    

    
}


-(void)config{
   
    self.navigationController.navigationBarHidden = YES;

    
    self.view.userInteractionEnabled = YES;
 
    
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    backImage.image = [UIImage imageNamed:@"backImageOfUser.jpeg"];
    backImage.alpha = 0.5;
    [self.view addSubview:backImage];
}

 // 上半部分登录界面
-(void)creatUpHalf{
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(SPACED * 3, SPACED * 2, SPACED * 3, SPACED * 3);
    [backButton setBackgroundImage:[UIImage imageNamed:@"ccArrow.png"] forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(backPop:) forControlEvents:UIControlEventTouchUpInside];
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    self.userNameLabel.text =userName;
    
    
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH / 2 - (SPACED * 8 + 6) / 2, SPACED * 10, SPACED * 8 + 6, SPACED * 8 + 6)];
    backView.layer.cornerRadius =(SPACED * 8 + 6) / 2;
    backView.layer.masksToBounds = YES;
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    // 登录按钮
    self.defaultLoadImageView = [[UIImageView alloc]init];
    self.defaultLoadImageView.frame = CGRectMake(0, 0, SPACED * 8, SPACED * 8);
    _defaultLoadImageView.center = backView.center;
    _defaultLoadImageView.layer.cornerRadius = SPACED * 4;
    _defaultLoadImageView.layer.masksToBounds = YES;
    [self.view addSubview:_defaultLoadImageView];
    
    
    // 用户设置
    UILabel *congfigUser = [[UILabel alloc]init];
    congfigUser.frame = CGRectMake(SCREENWIDTH - SPACED * 10, SPACED * 3, SPACED * 8, SPACED * 4);
    congfigUser.text = @"用户设置";
    congfigUser.textAlignment = 2;
    congfigUser.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:congfigUser];
    congfigUser.userInteractionEnabled = YES;
    
    
    
    // 点击手势
    UITapGestureRecognizer *defaultLoadTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerIsLoadOrNot)];
    defaultLoadTap.numberOfTapsRequired = 1;
    self.defaultLoadImageView.userInteractionEnabled = YES;
    [congfigUser addGestureRecognizer:defaultLoadTap];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    tap.numberOfTapsRequired = 1;
    [self.defaultLoadImageView addGestureRecognizer:tap];
 
    
    // 发布按钮
    self.publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_publishButton];
    [self.publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SPACED * 4, SPACED * 4));
        make.centerX.equalTo(self.view.mas_centerX).offset(-self.view.center.x / 2);
        make.top.equalTo(self.defaultLoadImageView.mas_bottom).offset(SPACED * 5);
        
    }];
    [self.publishButton setBackgroundImage:[UIImage imageNamed:@"publish@3x.png"] forState:UIControlStateNormal];
    // 我的发布
    UILabel *pulishLabel  = [[UILabel alloc]init];
    pulishLabel.font = [UIFont systemFontOfSize:15];
    pulishLabel.text = @"我的发布";
    pulishLabel.textAlignment = 1;
    [self.view addSubview:pulishLabel];
    [pulishLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SPACED * 10, SPACED * 4));
        make.centerX.equalTo(self.publishButton.mas_centerX);
        make.top.equalTo(self.publishButton.mas_bottom).offset(SPACED);
        
    }];
    
    
    
    // 我的收藏
    
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_collectionButton];
    [self.collectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SPACED * 4, SPACED * 4));
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.top.equalTo(self.defaultLoadImageView.mas_bottom).offset(SPACED * 5);
        
    }];
    [self.collectionButton setBackgroundImage:[UIImage imageNamed:@"collection@3x.png"] forState:UIControlStateNormal];
    
    UILabel *collectionLabel  = [[UILabel alloc]init];
    collectionLabel.font = [UIFont systemFontOfSize:15];
    collectionLabel.text = @"我的直播";
    collectionLabel.textAlignment = 1;
    [self.view addSubview:collectionLabel];
    [collectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(SPACED * 10, SPACED * 4));
        make.centerX.equalTo(self.collectionButton.mas_centerX);
        make.top.equalTo(self.collectionButton.mas_bottom).offset(SPACED);
        
    }];
    
    // 历史记录
    
    self.historyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_historyButton];
    [self.historyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SPACED * 4, SPACED * 4));
        make.centerX.equalTo(self.view.mas_centerX).offset(self.view.center.x / 2);
        make.top.equalTo(self.defaultLoadImageView.mas_bottom).offset(SPACED * 5);
        
    }];
    [self.historyButton setBackgroundImage:[UIImage imageNamed:@"history@3x.png"] forState:UIControlStateNormal];
    
    UILabel *historyLabel  = [[UILabel alloc]init];
    historyLabel.font = [UIFont systemFontOfSize:15];
    historyLabel.text = @"我的足迹";
    historyLabel.textAlignment = 1;
    [self.view addSubview:historyLabel];
    [historyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SPACED * 10, SPACED * 4));
        make.centerX.equalTo(self.historyButton.mas_centerX);
        make.top.equalTo(self.historyButton.mas_bottom).offset(SPACED);
        
    }];

    
    
}

// 用户名的懒加载
-(UILabel *)userNameLabel{
    
    
    
    if ([[EMClient sharedClient] isLoggedIn])
    {
    
    NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    // 判断是否已经有了, 若没有, 则进行实例化
      if (!_userNameLabel)
      {
        
        if (userName != nil)
        {
        _userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH / 2 - [Tools getTextWidth:userName withFontSize:15] / 2, SPACED * 6, [Tools getTextWidth:userName withFontSize:15] , SPACED * 2)];
            _userNameLabel.textAlignment = 1;
            _userNameLabel.font = [UIFont systemFontOfSize:15];
        [self.view addSubview:_userNameLabel];
        }
      }
    
    }
    return _userNameLabel;
}




-(void)tap{
    
    // 如果没有登录
    if(![EMClient sharedClient].isLoggedIn)
    {
        //跳转到登录和注册界面
        LoadResigter_ViewController *loadResigterVC = [[LoadResigter_ViewController alloc]init];
        [self presentViewController:loadResigterVC animated:YES completion:nil];
        
    }
    else
    {
        HadLogin_ViewController *hadLoginVC = [[HadLogin_ViewController alloc]init];
        UINavigationController *loginNavi = [[UINavigationController alloc]initWithRootViewController:hadLoginVC];
        
        [self presentViewController:loginNavi animated:YES completion:nil];
        
        
    }
 
    
}





-(void)headerIsLoadOrNot{
   
    // 如果没有登录
    if(![EMClient sharedClient].isLoggedIn)
    {
    //跳转到登录和注册界面
        LoadResigter_ViewController *loadResigterVC = [[LoadResigter_ViewController alloc]init];
        [self presentViewController:loadResigterVC animated:YES completion:nil];
        
    }
    else
    {
        HadLogin_ViewController *hadLoginVC = [[HadLogin_ViewController alloc]init];
        UINavigationController *loginNavi = [[UINavigationController alloc]initWithRootViewController:hadLoginVC];
        
        [self presentViewController:loginNavi animated:YES completion:nil];
    
        
    }
    
    
}

-(void)backPop:(UIButton *)backButton{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
